﻿@page "/Admin/Files"
@attribute [Authorize]
@attribute [TabItemOption(Text ="系统文件",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysFileService SysFileService

<div class="overflow-hidden h-100">
    <Table TItem="FileOutput"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="false"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowAddButton="false"
            ShowEditButton="false"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_search" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="250"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.FileLocation" />
            <TableColumn @bind-Field="@context.FileBucket" />
            <TableColumn @bind-Field="@context.FileOriginName" />
            <TableColumn @bind-Field="@context.FileSuffix" />
            <TableColumn @bind-Field="@context.FileSizeKb" />
            <TableColumn @bind-Field="@context.FileObjectName" />
        </TableColumns>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-3 col-xm-6">
                        <Select @bind-Value="@context.FileLocation" ShowLabel="true"/>
                    </div>
                    <div class="col-3 col-xm-6">
                        <BootstrapInput @bind-Value="@context.FileBucket" ShowLabel="true"/>
                    </div>
                    <div class="col-3 col-xm-6">
                        <BootstrapInput @bind-Value="@context.FileOriginName" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
        <TableToolbarTemplate>
            <ButtonUpload TValue="string" IsSingle="true" OnChange="@UpdateFileAsync" class="d-sm-inline-flex"></ButtonUpload>
        </TableToolbarTemplate>
        <RowButtonTemplate>
            <DropdownButton Text="更多" 
                Items="@(new List<DropdownButtonItem>()
                {
                    new("下载",()=> Task.CompletedTask),
                    new("详情",()=> Task.CompletedTask),
                    new("删除",()=> Task.CompletedTask),
                    new("预览",()=> Task.CompletedTask),
                })"/>
        </RowButtonTemplate>
    </Table>
</div>

@code{
    private Table<FileOutput>? _table;
    private FileOutput _search = new();

    private Task<QueryData<FileOutput>> OnQueryAsync(QueryPageOptions options)
        => SysFileService.PageAsync(_search.Adapt<FilePageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<FileOutput> items) 
        => SysFileService.DeleteAsync(items);

    private Task<bool> OnSaveAsync(FileOutput item, ItemChangedType type)
    {
        //var result = false;
        //switch (type)
        //{
        //    case ItemChangedType.Add:
        //        result = await SysFileService.AddAsync(item);
        //        break;
        //}
        //return result;

        return Task.FromResult(true);
    }

    private async Task UpdateFileAsync(UploadFile file)
    {
        var maxFileSize = 2 * 1024 * 1024;
        if (file?.File == null)
        {
            await Toast.Information("上传头像", "未检测到上传图片。");
            return;
        }

        if (file.Size > maxFileSize)
        {
            await Toast.Information("上传头像", "图片大小必须小于2MB。");
            file.Error = "图片大小超过2MB。";
            file.Code = 1;
            return;
        }

        var format = file.File.ContentType;
        if (!"jpg;png;bmp;gif;jpeg".Split(';').Any(x => format.Contains(x, StringComparison.OrdinalIgnoreCase)))
        {
            await Toast.Information("上传头像", "图片大小必须小于2MB。");
            file.Error = "图片大小超过2MB。";
            file.Code = 1;
            return;
        }

        try
        {
            var stream = file.File.OpenReadStream(maxFileSize);
            var content = new FormDataFile(stream, file.OriginFileName)
                {
                    ContentType = "image/png"
                };
            if (await SysFileService.UploadAsync(content) > 0)
            {
                await _table!.QueryAsync();
            }
        }
        catch (Exception ex)
        {
            file.Code = 1;
            file.Error = ex.Message;
        }
    }
}